import {observable, action, computed} from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
import * as API_WFREPORT from '../../apis/efficiencyReport';
import * as ReportUtil from "../../util/reportUtil";
import {ReportStore} from "./ReportStore";

const getLabel = WeaLocaleProvider.getLabel;

export class MostOverTimePersonStore extends ReportStore {
    labels = {};
    @observable advanceSearchPara = {
        orgtype: 3,
        orgid: '',
        datarange: 1,
        dataid: '',
        dateType: 1,
        startDate: '',
        endDate: '',
        tabKey: '1'
    }

    //modal
    @observable modalPara = {
        modalVisible: false,
        modalKey: '',
        modaltitle: '',
        dataParams: {},
        tabDatas: []
    }

    @computed get collectParams() {
        return {
            favouritetype: 2,
            importantlevel: 1,
            favname: this.labels.reportTitle,
            link: "wui/engine.html#/main/workflow/mostOvertimePersonAnalyse"
        }
    }

    constructor() {
        super();
        this.initDatas = this.initDatas.bind(this);
        this.loadAnalyseData = this.loadAnalyseData.bind(this);
        this.updateAdvanceSearchPara = this.updateAdvanceSearchPara.bind(this);
        this.showModal = this.showModal.bind(this);
        this.updateModalPara = this.updateModalPara.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    @action
    initDatas() {
        API_WFREPORT.getMOTPSearchCondition({}).then((data) => {
            const {noRight = false, rightKey = '-100', labels = {}, tabDatas = [], config = [], bottomInfo, reportid} = data;
            this.advanceSearchData = {noRight, tabDatas, config, bottomInfo, reportid};
            if (!noRight) {
                this.labels = labels;
                this.updateAdvanceSearchPara({rightKey});
            }
        });
    }

    loadAnalyseData() {
        this.loading = true;
        Promise.all([
            API_WFREPORT.getMOTPCountData(this.advanceSearchPara).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            }),
            API_WFREPORT.getMOTPEchartData(this.advanceSearchPara).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            })
        ]).then(() => {
            this.loading = false;
        });
    }

    updateAdvanceSearchPara(para) {
        let isload = true;
        let defaultInfo = {};
        let iddoupdate = false;
        for (let o in para) {
            if (this.advanceSearchPara[o] != para[o]) {
                iddoupdate = true;
                if (o === 'orgtype') {
                    const orgtype = para[o];
                    if (orgtype > 1) {
                        const {config = []} = this.advanceSearchData;
                        if (config.length > 0) {
                            const {selectLinkageDatas = {}} = config[0];
                            defaultInfo.orgid = ReportUtil.getDefaultId(orgtype, selectLinkageDatas);
                        }
                    }
                }
                if (o === 'datarange' && para[o] > 1) isload = false;
                if (o === 'datarange' && para[o] == 1) {
                    defaultInfo.dataid = '';
                }
                if (o === 'orgid' && para[o] == '') isload = false;
                if (o === 'dataid' && para[o] == '' && (para.datarange > 1 || (para.datarange > 1 && this.advanceSearchPara.datarange > 1))) {
                    isload = false;
                }
                if (o === 'dateType') {
                    if (para[o] == 6) {
                        isload = false;
                    } else {
                        defaultInfo.startDate = '';
                        defaultInfo.endDate = '';
                    }
                }
            }
        }
        if (iddoupdate) {
            this.advanceSearchPara = {...this.advanceSearchPara, ...para, ...defaultInfo};
            if (isload) {
                this.loadAnalyseData();
            }
        }
    }

    //type 1:更多 2:echart
    showModal(type, para = {}) {
        if (type == 1) {
            const {module = ''} = para;
            const {tabKey = 1} = this.advanceSearchPara;
            const dataParams = {
                'api': 'getMOTPDetailList',
                'module': module,
                ...this.advanceSearchPara
            }
            const clickParams = {module, 'analyseType': tabKey};
            this.modalPara = {
                modalVisible: true,
                modaltitle: this.labels[`echartTitle${module}_${tabKey}`],
                dataParams,
                isRowClick: true,
                clickParams
            }
        } else if (type == 2) {
            const {analyseType = '1', key, title = '', module} = para;
            const titleSuffix = this.labels[`lebelsuffix${module}_${analyseType}`];
            const dataParams = {
                'api': 'getMOTPEchartDetailList',
                'analyseType': analyseType,
                'keyid': key,
                'module': module,
                ...this.advanceSearchPara
            }
            const isRowClick = !(module == 2 && analyseType == '1');
            const clickParams = {'analyseType': (Number(analyseType) - 1), module};
            const bottomBtns = [];
            if (module == 1 && analyseType == '1') {
                const btnTitle = getLabel(383635, '超期流程清单');
                bottomBtns.push({module, analyseType: '0', btnTitle, parentKeyid: key});
            }
            this.modalPara = {
                modalVisible: true,
                modaltitle: `${titleSuffix}：${title}`,
                dataParams,
                isRowClick,
                clickParams,
                bottomBtns
            }
        }
    }

    /**
     * 更新modal框参数
     * @param para
     */
    updateModalPara(para = {}) {
        const {analyseType = 0, title = '', module = 1, keyid = '', parentKeyid = '', currentTime, columnDataIndex, clickNum = false} = para;
        let modaltitle = '';
        let isRowClick = true;
        const bottomBtns = [];
        const clickParams = {};
        let needAdvacnceSearch = analyseType == 0;
        let api = 'getMOTPEchartDetailList';
        if (clickNum) {
            modaltitle = getLabel(383302, '具体流程清单');
            isRowClick = false;
            needAdvacnceSearch = true;
            api = 'getMOTPRequestList';
        } else {
            if (analyseType == 0) {
                if (module == 1) {
                    modaltitle = getLabel(383635, '超期流程清单');
                } else {
                    modaltitle = getLabel(383302, '具体流程清单');
                }
                isRowClick = false;
            } else {
                const titleSuffix = this.labels[`lebelsuffix${module}_${analyseType}`];
                modaltitle = `${titleSuffix}：${title}`;
                isRowClick = !(module == 2 && analyseType == '1');
            }
            if (module == 1 && analyseType == '1') {
                const btnTitle = getLabel(383635, '超期流程清单');
                bottomBtns.push({module, analyseType: '0', btnTitle, parentKeyid: keyid});
            }
            clickParams.analyseType = (Number(analyseType) - 1);
            clickParams.module = module;
        }

        const dataParams = {
            api,
            module,
            analyseType,
            keyid,
            parentKeyid,
            currentTime,
            columnDataIndex,
            ...this.advanceSearchPara
        }

        const updateParam = {modaltitle, isRowClick, dataParams, clickParams, bottomBtns, needAdvacnceSearch};
        this.modalPara = {...this.modalPara, ...updateParam};
    }

    clearStatus(){
        this.advanceSearchPara = {
            orgtype: 3,
            orgid: '',
            datarange: 1,
            dataid: '',
            dateType: 1,
            startDate: '',
            endDate: '',
            tabKey: '1'
        }
        this.loading = false;
        this.advanceSearchData = {};
        this.analyseData = {};
    }
}